﻿using OpenClientX.Model;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;

namespace OpenClientX.Utils
{
    class CommontUtils
    {
        /// <summary>
        /// 检测是否有更新，比较当前版本号和数据库的版本号，你一样则说明有更新
        /// </summary>
        /// <param name="ServiceUrl"></param>
        /// <returns name="UpgradeTag">有更新返回true</returns>
        public static Boolean UpgradeTag(String ServiceUrl)
        {
            Boolean UpgradeTag = false;
            try
            {
                var versionv = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
                UpGradeModel versionModel = HttpUtil.getSysProperty("OpenClient_Version", ServiceUrl);
                //MessageBox.Show("获取版本号：" + versionModel.value+",当前版本号："+ versionv.ToString());
                if (versionv.ToString() != versionModel.value)
                {
                    UpgradeTag = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return UpgradeTag;
            }
            //MessageBox.Show("UpgradeTag:" + UpgradeTag);
            return UpgradeTag;
        }

        public static void DownloadUpgradeExe(String serviceUrl)
        {
            Dictionary<string, string> paramsDic = new Dictionary<string, string>();
            paramsDic.Add("account", "rr");
            paramsDic.Add("password", "rr");
            paramsDic.Add("key", "OpenClientX");
            HttpUtil.clientPost(serviceUrl + "/getActiveClient", paramsDic);

            Process p = Process.Start("C:/MssClientUpGrade/OpenClientX.msi");
        }

        /// <summary>
        /// 清除缓存的图像
        /// </summary>
        public static void clearCache()
        {
            #region
            String path = @"C:\MssClientCache\barcodeCache";     //扫描图像缓存
            try
            {
                DirectoryInfo dir = new DirectoryInfo(path);
                FileSystemInfo[] fileinfo = dir.GetFileSystemInfos();  //返回目录中所有文件和子目录

                foreach (FileSystemInfo i in fileinfo)
                {
                    if (i is DirectoryInfo)            //判断是否文件夹
                    {
                        DirectoryInfo subdir = new DirectoryInfo(i.FullName);
                        subdir.Delete(true);          //删除子目录和文件
                    }
                    else
                        File.Delete(i.FullName);      //删除指定文件
                }
            }
            catch (Exception ex)
            {
                return;
            }
            #endregion
        }

        static Object writerLogLocker = new object();
        /// <summary>  
        /// 将异常打印到LOG文件  
        /// </summary>  
        /// <param name="ex">异常</param>  
        /// <param name="LogAddress">日志文件地址</param>  
        public static void WriteLog(Exception ex)
        {
            #region
            lock (writerLogLocker)
            {
                string logPath = @"C:\MssClientCache\log\";
                //如果日志文件为空，则默认在Debug目录下新建 YYYY-mm-dd_Log.log文件  
                if (!Directory.Exists(logPath))
                {
                    Directory.CreateDirectory(logPath);
                }
                String LogAddress = @"C:\MssClientCache\log\" +
                            DateTime.Now.Year + '-' +
                            DateTime.Now.Month + '-' +
                            DateTime.Now.Day + "_OpenClientX_Log.txt";

                //把异常信息输出到文件
                StreamWriter fs = new StreamWriter(LogAddress, true);
                fs.WriteLine("异常时间：" + DateTime.Now.ToString());
                fs.WriteLine("异常信息：" + ex.Message);
                fs.WriteLine("异常对象：" + ex.Source);
                fs.WriteLine("调用堆栈：\n" + ex.StackTrace.Trim());
                fs.WriteLine("触发方法：" + ex.TargetSite);
                fs.WriteLine();
                fs.Close();
            }
            #endregion
        }
    }
}
